/******************************************************************************
 * vim:fdl=0:fdo=insert:
 *
 * Copyright (C), 2008-2009, iHao.Net.
 *
 * Project: iSite
 *
 * Filename: core.js
 *
 * Description: Just for fun
 *
 * Needs: google api, jQuery 1.2.6
 *
 * History:
 *   1. Date: 2009.01.07 20:04:01
 *      Author: iHao
 *      Modification: Scaffold basic functions
 *   2. Date: 2009.01.08 02:00:00
 *      Author: iHao
 *      Modification: add 'now' page [load from fanfou feed]
 *                    add C.F.getnow()
 *   3. Date: 2009.01.08 18:01:00
 *      Author: iHao
 *      Modification: add 'blog' page [load from blogger feed]
 *                    add C.F.getblog()
 *                    C.F.load() changed
 *   4. Date: 2009.01.08 21:01:00
 *      Author: iHao
 *      Modification: add 'photo' page [load from flickr json]
 *                    add C.F.getphotos()
******************************************************************************/
var C = {
	O : { //{{{ options
		t : null
	},//}}}
	I : function (options) {//{{{init
		$.extend(C.O,options);
		C.F.init();
	},//}}}
	F : {
		init : function () {//{{{init
			location.hash ? (function(C){C.F.load(location.hash);})(C) : (function(){C.F.load('#now');})(C);
		},//}}}
		load : function (n,obj) {//{{{load
			!obj&&(obj = $('a[href='+n+']'));
			obj.blur();
			if ($(obj).hasClass('current')) return;
			C.F.showLoading();
			n = n.substring(1);
			$(obj).siblings().removeClass('current');
			$(obj).addClass('current');
			try {
				eval('C.F.get'+n)();
			} catch (e) {
				C.F.writeContent(C.F.getContent(n));
				C.F.hideLoading();
			} finally {
				n = obj = null;
			}
			return;
		},//}}}
		writeContent : function () {//{{{writeContent
			var obj = arguments[1]?arguments[1]:'.block';
			$(obj)[0].innerHTML = arguments[0];
			arguments[2]&&(C.F.hideLoading());
		},//}}}
		showLoading : function () {//{{{showLoading
			C.O.t == null ? C.O.t = null : clearTimeout(C.O.t);
			$('a.run').show();
		},//}}}
		hideLoading : function () {//{{{hideLoading
			C.O.t == null ? C.O.t = null : clearTimeout(C.O.t);
			C.O.t = setTimeout(function(){$('a.run').hide();},1000);
		},//}}}
		getContent : function (n) {//{{{getContent
			var t = C.F.getTpl(n);
			var d = C.F.getData(n);
			try {
				return C.F.render(t,d);
			} catch (e) {
				return e||'<var>ERROR:Shit happend!</var>';
			} finally {
				t = d = n = null;
			}
		},//}}}
		getTpl : function (t) {//{{{getTpl
			try {
				return eval('C.T.'+t)||'<var>ERROR:No template named '+t+' !</var>';
			} finally {
				t = null;
			}
		},//}}}
		getData : function (d) {//{{{getData
			try {
				return eval('C.D.'+d)||'<var>ERROR:No data width '+d+' !</var>';
			} finally {
				d = null;
			}
		},//}}}
		render : function (t,d) {//{{{render
			try {
				var o = t.replace(/###head###/g,d.head);
				o = o.replace(/###time###/g,d.time);
				o = o.replace(/###data###/g,d.data);
				return o||'<var>ERROR:Rending template with data error!</var>';
			} finally {
				t = d = o = null;
			}
		},//}}}
		getnow : function () {//{{{getnow
			//get contents from twitter
			C.F.writeContent(C.T.now);
			var url = "http://api.fanfou.com/statuses/user_timeline/ihao.rss";
			var o = ['<ul>'];
			var feed = new google.feeds.Feed(url);
			feed.setNumEntries(20);
			feed.load(function(result) {
				if (!result.error) {
					$.each(result.feed.entries, function(i, entry) {
						var cls = (i%2==0)?'even':'odd';
						var num = ((i+1)<10)?('0'+''+(i+1)):(i+1);
						o[o.length] = '<li class="'+cls+'" onmouseover="C.F.liOver(this)" onmouseout="C.F.liOut(this)">';
						o[o.length] = '<i>'+num+'.</i>';
						o[o.length] = '<a class="now_link" href="'+entry.link+'">';
						o[o.length] = entry.title.replace('ihao','<strong>ihao</strong>');
						o[o.length] = '</a>';
						o[o.length] = '<span class="now_date">';
						o[o.length] = C.F.toDate(entry.publishedDate);
						o[o.length] = '</span>';
						o[o.length] = '</li>';
						cls = num = null;
					});
				} else {
					o[o.length] = '<li><var>ERROR:Loading NOW data error!</var></li>';
				}
				o[o.length] = '</ul>';
				C.F.writeContent(o.join(''),'.nowlist',true);
				url = o = null;
			});
		},//}}}
		getblog : function () {//{{{getblog
			//get contents from oedeo.blogspot.com
			C.F.writeContent(C.T.blog);
			var url = "http://oedeo.blogspot.com/feeds/posts/default";
			var o = ['<ul>'];
			var feed = new google.feeds.Feed(url);
			feed.setNumEntries(20);
			feed.load(function(result) {
				if (!result.error) {
					$.each(result.feed.entries, function(i, entry) {
						var cls = (i%2==0)?'even':'odd';
						var num = ((i+1)<10)?('0'+''+(i+1)):(i+1);
						o[o.length] = '<li class="'+cls+'" onmouseover="C.F.liOver(this)" onmouseout="C.F.liOut(this)" onclick="C.F.showorhide(this)">';
						o[o.length] = '<i>'+num+'.</i>';
						o[o.length] = '<strong>'+entry.author+'</strong>';
						o[o.length] = '<a class="now_link" href="'+entry.link+'">';
						o[o.length] = entry.title;
						o[o.length] = '</a>';
						o[o.length] = '<span class="now_date">';
						o[o.length] = C.F.toDate(entry.publishedDate);
						o[o.length] = '</span>';
						o[o.length] = '<div class="hides">'+entry.content+'</div>';
						o[o.length] = '</li>';
						cls = num = null;
					});
				} else {
					o[o.length] = '<li><var>ERROR:Loading BLOG data error!</var></li>';
				}
				o[o.length] = '</ul>';
				C.F.writeContent(o.join(''),'.bloglist',true);
				url = o = null;
			});
		},//}}}
		getshare : function () {//{{{getshare
			//get contents from http://www.google.com/reader/shared/03155843597492348954
			C.F.writeContent(C.T.share);
			var url = "http://www.google.com/reader/public/atom/user%2F03155843597492348954%2Fstate%2Fcom.google%2Fbroadcast";
			var o = ['<ul>'];
			var feed = new google.feeds.Feed(url);
			feed.setNumEntries(20);
			feed.load(function(result) {
				if (!result.error) {
					$.each(result.feed.entries, function(i, entry) {
						var cls = (i%2==0)?'even':'odd';
						var num = ((i+1)<10)?('0'+''+(i+1)):(i+1);
						o[o.length] = '<li class="'+cls+'" onmouseover="C.F.liOver(this)" onmouseout="C.F.liOut(this)" onclick="C.F.showorhide(this)">';
						o[o.length] = '<i>'+num+'.</i>';
						o[o.length] = '<strong>'+entry.author+'</strong>';
						o[o.length] = '<a class="now_link" href="'+entry.link+'">';
						o[o.length] = entry.title;
						o[o.length] = '</a>';
						o[o.length] = '<span class="now_date">';
						o[o.length] = C.F.toDate(entry.publishedDate);
						o[o.length] = '</span>';
						o[o.length] = '<div class="hides">'+entry.content+'</div>';
						o[o.length] = '</li>';
						cls = num = null;
					});
				} else {
					o[o.length] = '<li><var>ERROR:Loading SHARE data error!</var></li>';
				}
				o[o.length] = '</ul>';
				C.F.writeContent(o.join(''),'.bloglist',true);
				url = o = null;
			});
		},//}}}
		getphotos : function () {//{{{getphotos
			//get contents from ihao.yupoo.com
			C.F.writeContent(C.T.photos);
			var url = 'http://api.flickr.com/services/feeds/photos_public.gne?id=69772409@N00&lang=en-us&format=json&jsoncallback=?';
			var o = ['<ul>'];			
			$.getJSON(url,function(data) {
				if (data) {
					$('li.comments').html('Last Update:'+data.modified.substring(0,10));
					$.each(data.items, function(i, item) {
						o[o.length] = '<li onmouseover="C.F.liOver(this)" onmouseout="C.F.liOut(this)" onclick="C.F.showorhide(this)">';
						o[o.length] = '<a class="now_link" href="'+item.link+'" style="z-index:'+(20-i)+'">';
						o[o.length] = '<img src="'+item.media.m.replace('_m','_s')+'" />';
						o[o.length] = '<strong>'+item.title+'</strong>';
						o[o.length] = '</a>';
						o[o.length] = '</li>';
					});
				} else {
					o[o.length] = '<li><var>ERROR:Loading PHOTO data error!</var></li>';
				}
				o[o.length] = '<div class="clear"></div></ul>';
				C.F.writeContent(o.join(''),'.photoslist',true);
				url = o = null;
			});
		},//}}}
		showorhide : function (obj) {//{{{showorhide
			($(obj).find('.hides:hidden').length>0)?(function(o){
				$(o).find('.hides').slideDown();
			})(obj):(function(o){
				$(o).find('.hides').slideUp();
			})(obj);
		},//}}}
		toDate : function (d) {//{{{toDate
			return new Date(d).toLocaleString();
		},//}}}
		liOver : function (obj) {//{{{liOver
			$(obj).addClass('hover');
		},//}}}
		liOut : function (obj) {//{{{liOut
			$(obj).removeClass('hover');
		}//}}}
	},
	D : {//{{{Datas
		'comments' : {
			'head':'Give me five',
			'data':'"COMMENTS" page is in Construction...',
			'time':'09.01.07'
		},
		'about' : {
			'head':'About this guy',
			'data':'"ABOUT" page is in Construction...',
			'time':'09.01.07'
		}
	},//}}}
	T : {//{{{Templates
		'now' : '<h2>What i\'m doing now</h2><ul class="blockmenu"><li class="time"><a href="http://fanfou.com/ihao" target="_blank">read more...</a></li><li class="comments">Last Update:-</li></ul><div class="datas nowlist">loading now\'s entries...</div><ul class="blockmenu"><li class="tags"></li><li class="more"><a href="http://fanfou.com/ihao" target="_blank">read more...</a></li></ul>',
		'blog' : '<h2>Fresh blog\'s entries</h2><ul class="blockmenu"><li class="time"><a href="http://oedeo.blogspot.com/" target="_blank">read more...</a></li><li class="comments">Last Update:-</li></ul><div class="datas bloglist">loading blog\'s entries...</div><ul class="blockmenu"><li class="tags"></li><li class="more"><a href="http://oedeo.blogspot.com/" target="_blank">read more...</a></li></ul>',
		'share' : '<h2>My google reader\'s shares</h2><ul class="blockmenu"><li class="time"><a href="http://www.google.com/reader/shared/03155843597492348954" target="_blank">read more...</a></li><li class="comments">Last Update:-</li></ul><div class="datas bloglist">loading share\'s entries...</div><ul class="blockmenu"><li class="tags"></li><li class="more"><a href="http://www.google.com/reader/shared/03155843597492348954" target="_blank">read more...</a></li></ul>',
		'photos' : '<h2>Yes, It\'s me</h2><ul class="blockmenu"><li class="time"><a href="http://ihao.yupoo.com/" target="_blank">read more...</a></li><li class="comments">Last Update:-</li></ul><div class="datas photoslist">loading photos\'s entries...</div><ul class="blockmenu"><li class="tags"></li><li class="more"><a href="http://ihao.yupoo.com/" target="_blank">read more...</a></li></ul>',
		'comments' : '<h2>###head###</h2><ul class="blockmenu"><li class="time"></li><li class="comments">Last Update:###time###</li></ul><div class="datas">###data###</div><ul class="blockmenu"><li class="tags"></li><li class="more"></li></ul>',
		'about' : '<h2>###head###</h2><ul class="blockmenu"><li class="time"></li><li class="comments">Last Update:###time###</li></ul><div class="datas">###data###</div><ul class="blockmenu"><li class="tags"></li><li class="more"></li></ul>'
	}//}}}
};
//{{{other
var loadBlogbuzz = function() {
	$.getJSON(
		'http://pipes.yahoo.com/pipes/pipe.run?_id=Ys9dPOlw3RG5hXDXPxJ3AQ&_render=json&_callback=?',
		function(data) {
		  var container = $('#blogbuzz ul');
		  container.html('');
		  $.each(data.value.items, function(i, item) {
			  var li = $('<li/>').appendTo(container);
			  var a = $('<a/>').attr({
					'href' : item.link,
					'title' : item.title
				  }).text(item.title).appendTo(li);
			  $('<p/>').attr({
					'class' : 't-snippet'
				  }).html(item.description.replace('ihao','<span class="b-highlight">ihao</span>')).appendTo(li);
		  });
	});
};
function loadSearch() {
	var options = new GsearcherOptions();
	options.setExpandMode(GSearchControl.EXPAND_MODE_OPEN);
	var searchControl = new google.search.SearchControl();
	searchControl.addSearcher(new google.search.WebSearch(),options);
	//searchControl.addSearcher(new google.search.BlogSearch(),options);
	searchControl.draw(document.getElementById("resoult"));
	searchControl.execute("ihao press");
};
//}}}
